package no.kolstad.movies.service;
/**
 *  Copyright [2008] Christopher Kolstad
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software distributed under the License 
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License. 
 */

import no.kolstad.movies.model.Role;
import no.kolstad.movies.dao.RoleDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RoleService {
  private RoleDAO rDao;
  public void setRoleDAO(RoleDAO dao) {
    this.rDao = dao;
  }
  protected Log log = LogFactory.getLog(this.getClass());
  
  /**
   * Creates a role if it does not already exist
   * @param displayname - The display name of the role
   * @param rolename - The unique name of the role
   */
  public void createRole(String displayname, String rolename) {
    if(rDao.getRole(rolename) == null) {
      if(log.isDebugEnabled()) {
        log.debug(rolename +" did not exist. Creating it");
      }
      Role newRole = new Role();
      newRole.setDisplayname(displayname);
      newRole.setRolename(rolename);
      if(log.isDebugEnabled()) {
        log.debug("Saving role: " +newRole);
      }
      rDao.save(newRole);
    }
  }
}

